/*
 * @Author: silence
 * @Date: 2023-08-16 11:32:05
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-11-06 14:39:10
 * @Description: 登录-遵守协议view
 */
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../common/Method/CommonMethod.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../../utils/sputils/SpUtils.dart';
import '../../../utils/string/StringConstant.dart';
import '../../Mine/Language/SilLocalizations/SilLocalizations.dart';

class LoginProtocolWidget extends StatefulWidget {
  const LoginProtocolWidget({super.key});

  @override
  State<LoginProtocolWidget> createState() => LoginProtocolWidgetState();
}

class LoginProtocolWidgetState extends State<LoginProtocolWidget> {
  // 是否遵守协议
  bool _isAllowed = false;
  bool get isAllowed => _isAllowed;

  @override
  void initState() {
    super.initState();
    _isAllowed = SpUtils.getBoolData(StringConstant.AgreementAndPrivacyPolicy);
  }

  @override
  Widget build(BuildContext context) {
    return protocolWidget();
  }

  /// 协议
  protocolWidget() {
    String? agreedProtocol = LangOf(context)?.login?.agreedProtocol;
    String userAgreement =
        LangOf(context)?.login?.userAgreement ?? 'user agreement';
    String and = LangOf(context)?.login?.and ?? "and";
    String privacyPolicy =
        LangOf(context)?.login?.privacyPolicy ?? 'privacy policy';

    return Container(
      margin: EdgeInsets.only(bottom: 20.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SilButton(
            imgPath: _isAllowed
                ? "imgs/login/login_radio_sel.png"
                : "imgs/login/login_radio_nor.png",
            imgHeight: 16.w,
            imgWidth: 16.w,
            onTapFunc: () {
              setState(() {
                _isAllowed = !_isAllowed;
                SpUtils.saveBoolData(
                    StringConstant.AgreementAndPrivacyPolicy, _isAllowed);
              });
            },
          ),
          Container(
            width: 6.w,
          ),
          Expanded(
              child: RichText(
            text: TextSpan(
              style: TextStyle(
                fontSize: 14.sp,
                color: Color(0xFF808080),
              ),
              children: [
                TextSpan(text: agreedProtocol),
                _userAgreementSpan(" $userAgreement $and $privacyPolicy"),
                // TextSpan(text: " $and "),
                // _privacyPolicySpan(" $privacyPolicy "),
              ],
            ),
          )),
        ],
      ),
    );
  }

  /// 《用户协议》
  TextSpan _userAgreementSpan(String text) => TextSpan(
      text: text,
      style: TextStyle(
          fontSize: 14.sp,
          color: ColorUtils.color_theme,
          decoration: TextDecoration.underline),
      recognizer: TapGestureRecognizer()
        ..onTap = () => gotoServerTocolPage(context));

  /// 《隐私政策》
  TextSpan _privacyPolicySpan(String text) => TextSpan(
      text: text,
      style: TextStyle(
          fontSize: 14.sp,
          color: ColorUtils.color_theme,
          decoration: TextDecoration.underline),
      recognizer: TapGestureRecognizer()
        ..onTap = () => gotoProTocolPrivacyPage(context));

  /// 提示
  void showDialog({Function? agreeHandler}) {
    String? cancelButtonTitle = LangOf(context)?.login?.notYet;
    String? confrimButtonTitle = LangOf(context)?.login?.agree;
    String? promptContent = LangOf(context)?.login?.notAgreePrompt;
    String userAgreement =
        LangOf(context)?.login?.userAgreement ?? 'user agreement';
    String and = LangOf(context)?.login?.and ?? "and";
    String privacyPolicy =
        LangOf(context)?.login?.privacyPolicy ?? 'privacy policy';

    SilAlertDialog.showAlertDialog(
        context,
        SilAlertDialogData(
            // title: '温馨提示',
            cancelButtonTitle: cancelButtonTitle,
            confrimButtonTitle: confrimButtonTitle),
        contentWidget: Container(
          child: RichText(
              text: TextSpan(
                  text: promptContent,
                  style: TextStyle(
                      fontSize: 14.sp,
                      color: ColorUtils.color_subtitle,
                      height: 1.5.w),
                  children: [
                _userAgreementSpan(" $userAgreement $and $privacyPolicy"),
                // TextSpan(text: " $and "),
                // _privacyPolicySpan(" $privacyPolicy "),
              ])),
        ), confrimFunc: () {
      setState(() {
        _isAllowed = true;
        SpUtils.saveBoolData(
            StringConstant.AgreementAndPrivacyPolicy, _isAllowed);
      });
      if (agreeHandler != null) agreeHandler();
    });
  }
}
